package com.huaxin.hxmodulesystem.config;

import com.huaxin.hxmodulesystem.dto.request.AddSysUserReqDTO;
import com.huaxin.hxmodulesystem.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class AdminAccountInitializer {
    private static final Logger logger = LoggerFactory.getLogger(AdminAccountInitializer.class);


    private final SysUserService userService;

    @EventListener(ApplicationReadyEvent.class)
    public void initAdminAccount() {
        try {
            if (userService.queryAccountExist("admin")) {
                logger.info("管理员账号已存在。");
                return;
            }

            userService.initAdminAccount();
            logger.info("管理员账号创建成功。");
        } catch (Exception e) {
            logger.error("初始化管理员账号失败: ", e);
        }
    }
}
